package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.RegisterDAO;
import com.wanmait.hotelManage.dao.RoomDAO;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Register;

@WebServlet("/manage/CheckOutServlet")
public class CheckOutServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if("list".equals(action)) {
			this.list(request, response);
		}else if("delete".equals(action)) {
			this.delete(request, response);
		}else if("getRegisters".equals(action)) {
			this.getRegisters(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}
	}
	public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String roomId = request.getParameter("roomId");
		Register register=RegisterDAO.getRegisterdao().findRegisterByRoomId(Integer.parseInt(roomId));
		if(register==null) {
			PrintWriter out = response.getWriter();
			out.print("false");
		}else {
			request.setAttribute("register", register);
			request.getRequestDispatcher("/manage/room/showRegister.jsp").forward(request, response);
		}
		
	}
	//分页
	public void getRegisters(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pageNo = request.getParameter("pageNo");
		Pager pager=new Pager();
		pager.setPageSize(8);
		pager.setPageNo(Integer.parseInt(pageNo));
		int rows = RegisterDAO.getRegisterdao().getCheckinRows();
		pager.setPageCount(rows);
		
		List<Register> registers = RegisterDAO.getRegisterdao().findAllCheckinRegister(pager);
		request.setAttribute("pager", pager);
		request.setAttribute("registers", registers);
		request.getRequestDispatcher("/manage/room/showRegisters.jsp").forward(request, response);
	}
	//退房方法
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String roomId = request.getParameter("roomId");
		
		RegisterDAO.getRegisterdao().delete(Integer.parseInt(roomId));
		RoomDAO.getRoomdao().update(Integer.parseInt(roomId));
		RegisterDAO.getRegisterdao().updateEndTime(roomId);
		
		this.getRegisters(request, response);
	}
	//超找第一页的退房信息
	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Pager pager =new Pager();
		pager.setPageSize(8);
		pager.setPageNo(1);
		int rows=RegisterDAO.getRegisterdao().getCheckinRows();
		pager.setPageCount(rows);
		request.setAttribute("pager", pager);
		System.out.println(pager.getPageCount());
		
		List<Register> registers = RegisterDAO.getRegisterdao().findAllCheckinRegister(pager);
		request.setAttribute("registers", registers);
		request.getRequestDispatcher("/manage/room/checkOut.jsp").forward(request, response);
	}
}
